/*
 * Copyright (C) 2017 yixun All Rights Reserved.
 * 
 * ProjectPremisesIndustDataServiceImpl.java
 */
package com.emm.yixun.basic.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.emm.yixun.basic.mapper.ProjectPremisesIndustDataMapper;
import com.emm.yixun.basic.service.ProjectPremisesIndustDataService;
import com.emm.yixun.common.model.ProjectPremisesIndustData;
import com.emm.yixun.common.model.dto.ProjectPremisesIndustDataDto;
import com.emm.yixun.common.model.pmo.LevelStaticIndustMark;
import com.emm.yixun.common.model.pmo.LevelStaticIndustMarkParam;
import com.emm.yixun.common.page.Page;

/**
 * [文件名称]<br>
 * ProjectPremisesIndustDataServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 业态数据 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2017-03-09 10:57:03 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("projectPremisesIndustDataService")
public class ProjectPremisesIndustDataServiceImpl implements ProjectPremisesIndustDataService {

	@Autowired
	private ProjectPremisesIndustDataMapper mapper;

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<ProjectPremisesIndustData> getPageModel(ProjectPremisesIndustDataDto entity, int begin, int end) {
		Integer total = mapper.count(entity);
		List<ProjectPremisesIndustData> rows = mapper.list(entity, begin, end);
		Page<ProjectPremisesIndustData> msr = new Page<ProjectPremisesIndustData>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public List<ProjectPremisesIndustData> selectListByDto(ProjectPremisesIndustDataDto entity) {
		return mapper.selectListByDto(entity);
	}

	/**
	 * 查询单个
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public ProjectPremisesIndustData selectByDto(ProjectPremisesIndustDataDto entity) {
		return mapper.selectByDto(entity);
	}

	/**
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public ProjectPremisesIndustData selectById(Long id) {
		if (id != null) {
			return mapper.selectById(id);
		}
		return null;
	}

	/**
	 * @param entity
	 *            新增记录的详细信息
	 * @return 新增行数
	 */
	public ProjectPremisesIndustDataDto create(ProjectPremisesIndustDataDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		entity.setDelFlag("0");
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity
	 *            更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(ProjectPremisesIndustDataDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}

	/**
	 * @param entity
	 *            待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(ProjectPremisesIndustDataDto entity) {
		return mapper.del(entity);
	}

	/**
	 * @param entity
	 *            新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(ProjectPremisesIndustDataDto entity) {
		if (entity != null && entity.getId() != null) {
			return update(entity);
		} else {
			create(entity);
			return 1;
		}
	}

	/**
	 * @param entity
	 *            根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	public ProjectPremisesIndustData selectBy(ProjectPremisesIndustData entity) {
		if (entity != null && entity.getId() != null) {
			return mapper.selectById(entity.getId());
		}
		return entity;
	}
	
	/**
	 * 根据项目ID或组团ID查询业态数据
	 * 
	 * @param projectId，premisesId
	 * @return ProjectPremisesIndustData
	 */
	public ProjectPremisesIndustData selectIndustData(Long projectId, Long premisesId) {
		return mapper.selectIndustData(projectId, premisesId);
	}

	public LevelStaticIndustMark levelStaticIndustMarkSum(
			LevelStaticIndustMarkParam param,List<String> parkIdsList) {
		// TODO Auto-generated method stub
		//查询所有业态
		List<ProjectPremisesIndustData> list=queryIndustMark(param);
		//汇总业态
		LevelStaticIndustMark sum=sumIndustMark(list,parkIdsList);
		return sum;
	}
	
	private  LevelStaticIndustMark sumIndustMark(List<ProjectPremisesIndustData> list,List<String> parkIds){
		LevelStaticIndustMark sum=new LevelStaticIndustMark();
		//自持面积，单位：万平方米
	    BigDecimal selfSustaiAcreag=BigDecimal.ZERO;
	    //栋数，单位：栋
	    Integer numberOfBuildi=Integer.valueOf(0);
	    //层数，单位：层
	    Integer numberLayers=Integer.valueOf(0);
	    //总户数，单位：户
	    Integer totalHouse=Integer.valueOf(0);
	    //可售面积，单位：万平方米
	    BigDecimal salableAcreag=BigDecimal.ZERO;
	    //地上可售面积，单位：万平方米
	    BigDecimal groundSalesAcreag=BigDecimal.ZERO;
	    //地下可售面积，单位：万平方米
	    BigDecimal undergSalesAcreag=BigDecimal.ZERO;
	    //单位是个汇总，如车位
	    BigDecimal numberOfPark=BigDecimal.ZERO;
	    
	    if(null!=list && list.size()>0){
	    	for(ProjectPremisesIndustData row : list){
	    		String industMark=row.getIndustMark();
	    		if(parkIds.contains(industMark)){
	    			//可售面积单位为个
	    			if(null!=row.getSalableAcreag())
	    			numberOfPark=numberOfPark.add(row.getSalableAcreag());
	    		}else{
	    			//可售面积单位为万平方米
	    			if(null!=row.getSalableAcreag())
	    				salableAcreag=salableAcreag.add(row.getSalableAcreag());
	    		}
	    		if(null!=row.getSelfSustaiAcreag())
	    		selfSustaiAcreag=selfSustaiAcreag.add(row.getSelfSustaiAcreag());
	    		
	    		if(null!=row.getGroundSalesAcreag())
	    			groundSalesAcreag=groundSalesAcreag.add(row.getGroundSalesAcreag());
	    		
	    		if(null!=row.getUndergSalesAcreag())
	    			undergSalesAcreag=undergSalesAcreag.add(row.getUndergSalesAcreag());
	    		
                if(null!=row.getNumberOfBuildi())
	    	        numberOfBuildi=numberOfBuildi+row.getNumberOfBuildi();
                if(null!=row.getNumberLayers())
	    	        numberLayers=numberLayers+row.getNumberLayers();
                if(null!=row.getTotalHouse())
                	totalHouse=totalHouse+row.getTotalHouse();
	    	}
	    }
	    
	    sum.setSelfSustaiAcreag(selfSustaiAcreag);
	    sum.setNumberOfBuildi(numberOfBuildi);
	    sum.setNumberLayers(numberLayers);
	    sum.setTotalHouse(totalHouse);
	    sum.setSalableAcreag(salableAcreag);
	    sum.setGroundSalesAcreag(groundSalesAcreag);
	    sum.setUndergSalesAcreag(undergSalesAcreag);
	    sum.setNumberOfPark(numberOfPark.intValue());
		return sum;
	}
	
	
	private  List<ProjectPremisesIndustData> queryIndustMark(LevelStaticIndustMarkParam param){
		ProjectPremisesIndustDataDto query=new ProjectPremisesIndustDataDto();
		if(null!=param.getProjectId()){
			query.setProjectId(param.getProjectId());
		}
		if(null!=param.getPremisesId()){
			query.setPremisesId(param.getPremisesId());
		}
		if(null!=param.getBuildingIdList()){
			String dataPermission=" and t.building_id in (";
			int count=0;
			for(Long id : param.getBuildingIdList()){
				if(count==0){
					dataPermission=dataPermission+id;
				}else{
					dataPermission=dataPermission+","+id;
				}
				count++;
			}
			dataPermission=dataPermission+")";
			if(count>0){
				query.setDataPermission(dataPermission);	
			}
		}
		List<ProjectPremisesIndustData> list=mapper.selectListByDto(query);
		return list;
	}
	
	public List<ProjectPremisesIndustData> selectSumListByDto(ProjectPremisesIndustDataDto projectPremisesIndustDataDto){
		return mapper.selectSumListByDto(projectPremisesIndustDataDto);
	}

	public List<ProjectPremisesIndustData> queryIndustByBuilding(Long projectId, Long premisesId,String industType) {
	    
	    return mapper.queryIndustByBuilding(projectId, premisesId,industType);
	}

	public List<ProjectPremisesIndustData> queryIndustSum(Long projectId, Long premisesId,String industType) {
	    
	    return mapper.queryIndustSum(projectId, premisesId,industType);
	}
	
	
}
